package com.zhou.jiancai.fragment.tablayoutFragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhou.jiancai.R;
import com.zhou.jiancai.Utils.LogUtils;
import com.zhou.jiancai.activity.DanmkuVideoActivity;
import com.zhou.jiancai.activity.GestureActivity;
import com.zhou.jiancai.activity.JCListActivity;
import com.zhou.jiancai.activity.ListVideo2Activity;
import com.zhou.jiancai.activity.MyMapActivity;
import com.zhou.jiancai.activity.NiceViewShowActivity;
import com.zhou.jiancai.activity.PopWindowActivity;
import com.zhou.jiancai.activity.QRcodeActivity;
import com.zhou.jiancai.activity.ShowcaseActivity;

import java.util.ArrayList;

import cn.bingoogolapple.bgabanner.BGABanner;


/**
 * Created by Administrator on 2017/8/15.
 */

@SuppressLint("ValidFragment")
public class RecommendFragment extends Fragment {
    public  RecommendFragment instance;
    public   RecommendFragment getInstance(){
        if (instance !=null) {
            return instance;

        }else {
            RecommendFragment instance1=new RecommendFragment(getActivity());
            return instance1;
        }
    }


    private ArrayList alist;
  public Activity mActivity;
    private SwipeRefreshLayout swipeRefreshLayout;
    private BGABanner vpshow;
    private ScrollView scrollView;
    private ArrayList list;
    private RecAdapter recAdapter;


    @SuppressLint("ValidFragment")
    public RecommendFragment(Activity activity) {
        mActivity=activity;
    }

    private ArrayList clist;
    private ArrayList blist;
    private RecyclerView recyclerView1;
    private BGABanner vp_banner;
    private ArrayList<ImageView> mImageViewList;
    static int[] pics = {R.drawable.ic_alert, R.drawable.ic_edit, R.drawable.ic_favorite, R.drawable.ic_search

    };
    private MyLayoutManager myLayoutManager;

    private static final int REFRESH_COMPLETE = 0X110;
    public static final int HEADER = 0;
    public static final int BODY = 1;
    private static final int HEADERTITLE =2 ;
   private Handler  mHandler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (  msg.what ==1) {
                for (int i = 0; i <5 ; i++) {
                    list.add(i);
                }
                LogUtils.d("msg.what:"+msg.what);

            recAdapter.notifyDataSetChanged();
                LogUtils.d("recadapter"+list.size());
                if (swipeRefreshLayout.isRefreshing()) {
                    swipeRefreshLayout.setRefreshing(false);


                }

            }

        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.layout_recommend, container, false);
        recyclerView1 = (RecyclerView) view.findViewById(R.id.recycle_product);


        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);


        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                Message msg=Message.obtain();
                msg.what=1;
          mHandler.sendMessageDelayed(msg,1000);
                LogUtils.d("msg发送");
            }
        });

        initRecycleViewData();
        initRecycleView();




        return view;
    }

    private void initRecycleViewData() {
        //getremoteData();
        list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
    }

    private void initRecycleView() {
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        //GridLayoutManager linearLayoutManager=new GridLayoutManager(getActivity(),2);
        recyclerView1.setLayoutManager(linearLayoutManager);
        // myLayoutManager = new MyLayoutManager(getActivity());
        // recyclerView1.setLayoutManager(myLayoutManager);

        // initRecycleViewHeaderForBanner();//初始化recycleview的头布局

        recAdapter = new RecAdapter();
        recyclerView1.setAdapter(recAdapter);
        recAdapter.setOnItemClickListener(new BodyViewHolder.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Intent i;
                Toast.makeText(mActivity, "点击了"+postion, Toast.LENGTH_SHORT).show();
                if (postion == 2) {
                  //  i=new Intent(getActivity(), JCListActivity.class);
                   // getActivity().startActivity(i);
                }
                if (postion == 3) {
                   // i=new Intent(getActivity(), QRcodeActivity.class);
                   // getActivity().startActivity(i);

                }
                if (postion == 4) {
                    i=new Intent(getActivity(), NiceViewShowActivity.class);
                    getActivity().startActivity(i);

                }
                if (postion == 5) {
                    //跳转可以缩放可拖动的视频控件 在listview滑动时出现
                    i=new Intent(getActivity(), ListVideo2Activity.class);
                    getActivity().startActivity(i);

                }
                if (postion == 6) {
                    //跳转带弹幕的视频
                    i=new Intent(getActivity(), DanmkuVideoActivity.class);
                    getActivity().startActivity(i);

                }

            }
        });


    }


    public class MyLayoutManager extends LinearLayoutManager {

        public MyLayoutManager(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }


        @Override
        public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
            int count = state.getItemCount();
            if (count >0) {
                View view = recycler.getViewForPosition(0);
                if (view != null) {
                    measureChild(view, widthSpec, heightSpec);
                    int measuredWidth = View.MeasureSpec.getSize(widthSpec);
                    int measuredHeight = view.getMeasuredHeight();
                    setMeasuredDimension(measuredWidth, measuredHeight);
                }
            } else {
                super.onMeasure(recycler, state, widthSpec, heightSpec);
            }
        }
    }


    public  class RecAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder>implements AdapterView.OnItemClickListener{
        //1 自定义子条目点击的接口
        private BodyViewHolder.MyItemClickListener mItemClickListener;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=null;
            //如果是头布局// 得传一个头进来直接这样写头头不会自动轮播
            if (viewType ==HEADER) {

                view = mActivity.getLayoutInflater().inflate(R.layout.life_header_vp,parent,false);
                return new HeaderViewHolder(view);
            }
            if (viewType ==HEADERTITLE) {
                view = mActivity.getLayoutInflater().inflate(R.layout.life_header_title,parent,false);
                return new HeaderTitleViewHolder(view);
            }
            //如果不是头
            if (viewType ==BODY) {
                view =mActivity.getLayoutInflater().inflate(R.layout.product_recycle_item,parent,false);


                return new BodyViewHolder(view,mItemClickListener);
            }


            return new BodyViewHolder(view,mItemClickListener)  ;
            //或者抛出异常
           // throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");

        }
        /**
         * 设置Item点击监听
         *
         * @param listener
         */
        //2对外提供公共的访问方法
        public void setOnItemClickListener(BodyViewHolder.MyItemClickListener listener) {
            this.mItemClickListener = listener;
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {


            if (holder instanceof  HeaderViewHolder) {
             ((HeaderViewHolder) holder).bgaBanner.setAdapter(new BGABanner.Adapter<ImageView,String>() {
                 @Override
                 public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                   itemView.setScaleType(ImageView.ScaleType.FIT_XY);
                   itemView.setImageResource(pics[position]);

                 }


             });
                ((HeaderViewHolder) holder).bgaBanner.setAutoPlayAble(true);//自动轮播
                ((HeaderViewHolder) holder).bgaBanner.setData(pics);
                ((HeaderViewHolder) holder).bgaBanner.setDelegate(new BGABanner.Delegate<ImageView,String>() {
                    @Override
                    public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                        Intent intent;
                        if (position == 0){
                            intent=new Intent(getActivity(),GestureActivity.class );
                            startActivity(intent);

                        }else if (position == 1){
                            intent=new Intent(getActivity(),ShowcaseActivity.class );
                            startActivity(intent);

                        }else
                        if (position == 2) {
                            intent = new Intent(getActivity(), PopWindowActivity.class);
                            startActivity(intent);

                        } else if(position == 3){
                            Toast.makeText(getActivity(), "高德地图" +
                                    "", Toast.LENGTH_SHORT).show();
                            intent = new Intent(getActivity(),MyMapActivity.class);
                            startActivity(intent);

                        }
                    }


                });
            }


            if (holder instanceof BodyViewHolder) {
                //这里判断左右条目,进行处理后显示
                ((BodyViewHolder) holder).textView_right.setText("我是条目"+position);
                ((BodyViewHolder) holder).textView_left.setText("我是条目"+position);






            /*    if (position%2 ==0) {

                }
                if (position%2 !=0) {
                    ((BodyViewHolder) holder).textView_right.setText("我是条目"+(position/2));
                    ((BodyViewHolder) holder).textView_left.setText("我是条目"+(position-1)/2);

                }
*/

            }

        }



        @Override
        public int getItemViewType(int position) {
            if (position==0) {
                return HEADER;

            }
            if (position ==1) {
                return HEADERTITLE;
            }
                return BODY;


        }

        @Override
        ///因为多了一个头部，所以是+1,但是头部 ViewPager 占了7个
        //所以实际是少了6个
        public int getItemCount() {
            return  list.size()+4-1;
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {

        private final BGABanner bgaBanner;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            bgaBanner = (BGABanner) itemView.findViewById(R.id.banner_guide_content);

        }
    }

    public static class BodyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        //3实现接口的调用
        //声明MyItemClickListener
        private MyItemClickListener mListener;

        private final TextView textView_left;
        private final TextView textView_right;


        public BodyViewHolder(View itemView ,MyItemClickListener mItemclicklistener) {
            super(itemView);
            textView_left = (TextView) itemView.findViewById(R.id.product_recycle_item_textview_left);

            textView_right = (TextView) itemView.findViewById(R.id.product_recycle_item_textview_right);
            itemView.setOnClickListener(this);

            mListener=mItemclicklistener;

        }
        //声明MyItemClickListener这个接口
        public interface MyItemClickListener {
            public void onItemClick(View view,int postion);

        }

        @Override
        public void onClick(View v) {
//如果mListener不为空，就实现接口中的方法onItemClick其中getPosition()是得到被点击位置的position
            if(mListener != null){
                mListener.onItemClick(v,getPosition());
            }
        }
    }




    private class HeaderTitleViewHolder extends RecyclerView.ViewHolder {
        public HeaderTitleViewHolder(View view) {
            super(view);

        }
    }

}
